<template>
  <div class="employees">
    <!-- 使用工具栏组件 -->
    <pageTools :showleft="true">
      <template #left> 员工管理 </template>
      <template #right>
      <el-button @click="toImport" type="primary" size="mini">批量导入</el-button>
        <el-button type="primary" size="mini" @click="openadd"
          >+新增员工</el-button
        >
      </template>
    </pageTools>

    <!-- 新增面板 -->
    <mypanel
      :dialogVisible.sync="dialogVisible"
      @updateData="getEmployeesList"
    ></mypanel>

    <!-- 表格区域 -->
    <el-card class="box-card">
      <el-table :data="tableData" border class="mytable">
        <el-table-column
          label="序号"
          type="index"
          :index="indexFn"
          width="80px"
        ></el-table-column>
        <el-table-column
          prop="username"
          width="120px"
          label="姓名"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          width="120px"
          label="手机号"
        ></el-table-column>
        <el-table-column
          prop="workNumber"
          width="120px"
          label="工号"
        ></el-table-column>
        <el-table-column prop="formOfEmployment" width="120px" label="聘用形势">
          <template slot-scope="{ row }">
            {{ row.formOfEmployment | myEmployees }}
          </template>
        </el-table-column>
        <el-table-column
          prop="departmentName"
          width="120px"
          label="部门"
        ></el-table-column>
        <el-table-column
          prop="timeOfEntry"
          width="120px"
          label="入职时间"
        ></el-table-column>
        <el-table-column width="120px" label="是否在职">在职</el-table-column>
        <el-table-column width="120px" label="状态">可用</el-table-column>
        <el-table-column width="400px" label="操作">
          <template>
            <el-button type="text">查看</el-button>
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text">角色</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getEmployeesListApi } from '@/api/employees'
export default {
  data () {
    return {
      // 表格的数据源
      tableData: [],
      // 当前页
      currentPage: 1,
      // 页容量的集合
      pageSizes: [5, 10, 15, 20],
      // 页容量
      pageSize: 10,
      // 总条数
      total: 0,
      // 控制面板的显示与隐藏
      dialogVisible: false
    }
  },
  methods: {
    // 跳转到导入页面
toImport () {
  this.$router.push('/employees/import')
},
    // 第二页的序号接上第一页的序号
    indexFn (index) {
      // 第一页=> 1
      // 第二页=> 2
      return (this.currentPage - 1) * this.pageSize + index + 1
    },

    // 打开新增面板
    openadd () {
      this.dialogVisible = true
    },

    // 当页容量改变时会触发
    handleSizeChange (size) {
      // console.log(`每页 ${size} 条`);
      ;(this.pageSize = size), this.getEmployeesList()
    },
    // 当前页改变时会触发
    handleCurrentChange (page) {
      // console.log(`当前页: ${page}`);
      ;(this.currentPage = page), this.getEmployeesList()
    },

    // 获取所有的员工信息
    async getEmployeesList () {
      const res = await getEmployeesListApi({
        page: this.currentPage,
        size: this.pageSize
      })
      console.log(res)
      // 保存数据
      this.tableData = res.data.rows
      this.total = res.data.total
    }
  },
  created () {
    // 得到所有的用户信息
    this.getEmployeesList()
  },
  components: {
    mypanel: () => import('./components/mypanel.vue')
  }
}
</script>

<style lang="scss">
.employees {
  margin: 20px;
  .box-card {
    margin-top: 20px;
  }
  .el-pagination {
    margin-top: 20px;
    text-align: right;
  }
}
</style>
